package com.horay.statemachiedemo.single.config;

import com.horay.statemachiedemo.single.enums.OrderState;
import com.horay.statemachiedemo.single.event.OrderEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.transition.Transition;

import java.util.EnumSet;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@Configuration
@EnableStateMachine
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<OrderState, OrderEvent> {

    @Override
    public void configure(StateMachineStateConfigurer<OrderState, OrderEvent> states) throws Exception {
        states.withStates().initial(OrderState.UNPAID).states(EnumSet.allOf(OrderState.class));
    }

    @Override
    public void configure(StateMachineTransitionConfigurer<OrderState, OrderEvent> transitions) throws Exception {
        transitions
                .withExternal().source(OrderState.UNPAID).target(OrderState.WAITING_FOR_RECEIVE).event(OrderEvent.PAY)
                .and()
                .withExternal().source(OrderState.WAITING_FOR_RECEIVE).target(OrderState.DONE).event(OrderEvent.RECEIVE);
    }

    @Override
    public void configure(StateMachineConfigurationConfigurer<OrderState, OrderEvent> config) throws Exception {
        config.withConfiguration().listener(listener());
    }

    @Bean
    public StateMachineListener<OrderState, OrderEvent> listener() {
        return new StateMachineListenerAdapter<OrderState, OrderEvent>() {

            @Override
            public void transition(Transition<OrderState, OrderEvent> transition) {
                if (transition.getTarget().getId() == OrderState.UNPAID) {
                    log.info("Listener:订单创建，待支付");
                    return;
                }

                if (transition.getSource().getId() == OrderState.UNPAID
                        && transition.getTarget().getId() == OrderState.WAITING_FOR_RECEIVE) {
                    log.info("Listener:用户完成支付，待收货");
                    return;
                }

                if (transition.getSource().getId() == OrderState.WAITING_FOR_RECEIVE
                        && transition.getTarget().getId() == OrderState.DONE) {
                    log.info("Listener:用户已收货，订单完成");
                    return;
                }
            }

        };
    }

}
